package com.zhuozhou.jdyc.main.main.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.TabItemBinding;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by CherishTang on 2020/3/27 0027
 * describe:首页菜单adapter
 */
public class IndexTabAdapter extends BaseRecyclerViewAdapter<TabItemBean, TabItemBinding> {

    public OnTabItemClickListener mOnTabItemClickListener;

    public IndexTabAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tab_item;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);
        baseViewHolder.getBinding().getRoot().setOnClickListener(v -> {
            List<TabItemBean> list = new ArrayList<>();
            for (TabItemBean item : mList) {
                if (item.getIsView()) {
                    item.setIsView(false);
                }
                list.add(item);
            }
            list.get(pos).setIsView(true);
            setList(list);
            notifyDataSetChanged();
            if (mOnTabItemClickListener != null) {
                mOnTabItemClickListener.onTabItemClick(baseViewHolder.getBinding().getRoot(), getRealPosition(baseViewHolder));
            }
        });
    }

    @Override
    public void onBindHolder(BaseViewHolder<TabItemBinding> holder, int pos) {
        holder.getBinding().tvTop.setText(mList.get(pos).getTopName());
        holder.getBinding().tvCenter.setText(mList.get(pos).getCenterName());
        if (mList.get(pos).getIsView()) {
            holder.getBinding().vBottom.setVisibility(View.VISIBLE);
            holder.getBinding().tvTop.setTextSize(18);
            holder.getBinding().tvTop.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            holder.getBinding().vBottom.setVisibility(View.GONE);
            holder.getBinding().tvTop.setTextSize(16);
            holder.getBinding().tvTop.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
    }

    public void setOnTabItemClickListener(OnTabItemClickListener l) {
        mOnTabItemClickListener = l;
    }

    public interface OnTabItemClickListener {
        void onTabItemClick(View view, int position);
    }

}
